package ar.com.rys.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginYahooServlet extends AbstractHttpServlet {
	
	private static final Logger log = Logger.getLogger(LoginYahooServlet.class.getName());
	
	static {
        openIdProviders = new HashMap<String, String>();
        openIdProviders.put("Yahoo", "yahoo.com");
    }
	
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		
		log.info("Iniciando metodo doget");
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		Set<String> attributes = new HashSet<String>();
		
				
		if (user != null) {
			
			if(checkIsRegistredUser(user)){
				try {
					resp.sendRedirect("secure/secureHome.html");
				} catch (Exception e) {
					log.severe(e.getMessage());
				}
			}else{
				try {
					resp.sendRedirect("secure/loguedin.html");
				} catch (Exception e) {
					log.severe(e.getMessage());
				}
			}
		
			
			
		} else {
			
			for (String providerName : openIdProviders.keySet()) {
                String providerUrl = openIdProviders.get(providerName);
                String loginUrl = userService.createLoginURL(req.getRequestURI(), null, providerUrl, attributes);
                log.info(loginUrl);
                resp.sendRedirect(loginUrl);
            }
			 
			
		}
	}
}